package com.mcxx.util;


import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class AreaUtil {
    public final static String HUNANCODE = "430000000000";

    public String getValidCode(String areaCode, int level) {
        switch (level){
            case Constant.AreaLevel.PROVINCE:
                return areaCode.substring(0, 2);
            case Constant.AreaLevel.CITY:
                return areaCode.substring(0, 4);
            case Constant.AreaLevel.COUNTY:
                return areaCode.substring(0, 6);
            case Constant.AreaLevel.STREET:
                return areaCode.substring(0, 9);
            case Constant.AreaLevel.COMMUNITY:
                return areaCode.substring(0, 12);
            default:
                return areaCode;
        }
    }

    public String getSuperLevelAreaCode(String areaCode, int level){
        switch (level){
            case Constant.AreaLevel.PROVINCE:
                return areaCode.substring(0, 2) + "0000000000";
            case Constant.AreaLevel.CITY:
                return areaCode.substring(0, 4) + "00000000";
            case Constant.AreaLevel.COUNTY:
                String selfArea = areaCode.substring(7, 9);
                if("999".equals(selfArea)) {
                    return areaCode.substring(0, 6) + "999000";
                }
                return areaCode.substring(0, 6) + "000000";
            case Constant.AreaLevel.STREET:
                return areaCode.substring(0, 9) + "000";
            case Constant.AreaLevel.COMMUNITY:
                return areaCode.substring(0, 12);
            default:
                return areaCode;
        }
    }

    public int getValidCodeLen(int level){
        switch (level){
            case Constant.AreaLevel.PROVINCE:
                return 2;
            case Constant.AreaLevel.CITY:
                return 4;
            case Constant.AreaLevel.COUNTY:
                return 6;
            case Constant.AreaLevel.STREET:
                return 9;
            case Constant.AreaLevel.COMMUNITY:
                return 12;
            default:
                return 15;
        }
    }


    public List<String> getNodeTreePathFromRoot(String areaCode){
        List<String> codes = new ArrayList<String>();
        if(null == areaCode || areaCode.length() != 12)
            return codes;

        codes.add(areaCode.substring(0, 2).concat("0000000000"));

        if(Double.parseDouble(areaCode.substring(2)) <= 0)
            return codes;

        codes.add(areaCode.substring(0, 4).concat("00000000"));
        if(Integer.parseInt(areaCode.substring(4)) <= 0)
            return codes;

        codes.add(areaCode.substring(0, 6).concat("000000"));
        if(Integer.parseInt(areaCode.substring(6)) <= 0)
            return codes;

        codes.add(areaCode.substring(0, 9).concat("000"));
        if(Integer.parseInt(areaCode.substring(9)) <= 0)
            return codes;

        codes.add(areaCode);
        return codes;
    }

    public String getValidCode(String areaCode) {
        if(null == areaCode || areaCode.length() > 12)
            return areaCode;

        if(areaCode.endsWith("0000000000"))
            return areaCode.substring(0, 2);

        if(areaCode.endsWith("00000000"))
            return areaCode.substring(0, 4);

        if(areaCode.endsWith("000000"))
            return areaCode.substring(0, 6);

        if(areaCode.endsWith("000"))
            return areaCode.substring(0, 9);

        return areaCode;
    }

    public int getLevelByAreaCode(String areaCode) {
        if(null == areaCode || areaCode.length() > 12)
            return Constant.AreaLevel.GROUP;

        if(areaCode.endsWith("0000000000"))
            return Constant.AreaLevel.PROVINCE;

        if(areaCode.endsWith("00000000"))
            return Constant.AreaLevel.CITY;

        if(areaCode.endsWith("000000"))
            return Constant.AreaLevel.COUNTY;

        if(areaCode.endsWith("000"))
            return Constant.AreaLevel.STREET;

        return Constant.AreaLevel.COMMUNITY;
    }
}
